package org.feifei.org.service.impl;

import org.feifei.basic.service.impl.BaseServiceImpl;
import org.feifei.org.domain.SystemDictionaryType;
import org.feifei.org.mapper.SystemDictionaryDetailMapper;
import org.feifei.org.mapper.SystemDictionaryTypeMapper;
import org.feifei.org.service.SystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author forever
 * @Description: 数据字典类型的service 层实现类
 * @date 2021/12/04 09:35
 */
@Service
public class SystemDictionaryTypeServiceImpl extends BaseServiceImpl<SystemDictionaryType> implements SystemDictionaryTypeService {

    @Autowired
    private SystemDictionaryTypeMapper systemDictionaryTypeMapper;

    @Autowired
    private SystemDictionaryDetailMapper systemDictionaryDetailMapper;

    /**
     * 根据是否有id来判断是 删除还是修改
     * @param systemDictionaryType
     */
    @Override
    @Transactional
    public void save(SystemDictionaryType systemDictionaryType) {
        if (systemDictionaryType.getId() != null) {
            systemDictionaryTypeMapper.update(systemDictionaryType);
        } else {
            systemDictionaryTypeMapper.save(systemDictionaryType);
        }
    }

    /**
     * 根据id来删除，需要先清空相关明细表的数据
     * @param id 数据字典类型的id
     */
    @Transactional
    @Override
    public void remove(Long id) {
        systemDictionaryDetailMapper.removeByTypeId(id);
        systemDictionaryTypeMapper.remove(id);
    }

    /**
     * 批量删除 需要先清空相关明细表的数据
     * @param ids 数据字典类型的id的集合
     */
    @Override
    @Transactional
    public void batchRemove(List<Long> ids) {
        if (ids.size() <= 0) {
            return;
        }
        systemDictionaryDetailMapper.batchRemoveByTypeId(ids);
        systemDictionaryTypeMapper.batchRemove(ids);
    }
}
